package com.hy;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TriangleDemoTest {
    private static TriangleDemo triangleDemo;

    @BeforeClass
    public void beforeClass() {
        System.out.println("BeforeClass方法");
        triangleDemo = new TriangleDemo();
    }

    @DataProvider
    public Object[][] dp1() {
        Object[][] objects = {{1, 2, 3, 4}, {2, 3, 4, 9}, {3, 4, 5, 6}};
        return objects;
    }

    @DataProvider
    public Object[][] dp2() throws IOException {
        File file = new File("src/com/hy/lib/triangle.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        List<Double[]> list = new ArrayList<>();
        String record;
        while ((record = bufferedReader.readLine()) != null) {
            String[] spilt = record.split(",");
            Double[] doubles = new Double[spilt.length];
            for (int i = 0; i < spilt.length; i++) {
                doubles[i] = Double.parseDouble(spilt[i]);
            }
            list.add(doubles);

            System.out.println(Arrays.toString(spilt));
        }
        Double[][] objects1 = new Double[list.size()][];

        for (int i = 0; i < list.size(); i++) {
            objects1[i] = list.get(i);
        }
        return objects1;
    }

    @DataProvider
    public Object[][] dp3() throws SQLException, IOException, ClassNotFoundException {
        return new JdbcTriangle().getCon("src/com/hy/lib/mys.txt");
    }

    @Test(dataProvider = "dp1")
    public void testPerimeter(double a, double b, double c, double excepted) {
        System.out.println("当边长为" + a + "," + b + "," + c + "时，" + "三角形的周长为：");
        System.out.println(triangleDemo.trianglePerimeter(a, b, c));
        Assert.assertEquals(excepted, triangleDemo.trianglePerimeter(a, b, c));
    }

    @Test(dataProvider = "dp2")
    public void testArea(double a, double b, double c, double excepted) {
        System.out.println("当边长为" + a + "," + b + "," + c + "时，" + "三角形的面积为：");
        System.out.println(triangleDemo.triangleArea(a, b, c));
        Assert.assertEquals(excepted, triangleDemo.triangleArea(a, b, c));
    }

    @Test(dataProvider = "dp3")
    public void testPerimeterJdbc(String ademo, String bdemo, String cdemo, String excepteddemo) {
        double a = Double.parseDouble(ademo);
        double b = Double.parseDouble(bdemo);
        double c = Double.parseDouble(cdemo);
        double excepted = Double.parseDouble(excepteddemo);
        System.out.println("当边长为" + a + "," + b + "," + c + "时，" + "三角形的周长为：");
        System.out.println(triangleDemo.trianglePerimeter(a, b, c));
        Assert.assertEquals(excepted, triangleDemo.trianglePerimeter(a, b, c));
    }
}


